﻿Imports System.Xml

Public NotInheritable Class UsedExpression

    Public Sub New(ByVal _xExpression As XElement)
        __Text = _xExpression.<Text>.Value()

        __Variables = New List(Of UsedExpressionVariable)()
        For Each _xVariable As XElement In _xExpression.<Variables>.<Variable>
            __Variables.Add(New UsedExpressionVariable(_xVariable))
        Next _xVariable

        If _xExpression.<Result>...<Value>.Count() = 1 Then
            __ResultValue = _xExpression.<Result>...<Value>(0).Value
            __ResultExceptionType = Nothing
        Else
            If _xExpression.<Result>...<Exception>.Count() = 1 Then
                __ResultValue = Nothing
                __ResultExceptionType = _xExpression.<Result>...<Exception>(0).Value
            End If
        End If
    End Sub

    Public Sub New(ByVal _expression As Enhancements.Basic.Expressions.Expression, ByVal _resultValue As Object, ByVal _resultExceptionType As String)
        __Text = _expression.RootToken.Expression

        __Variables = New List(Of UsedExpressionVariable)()
        For Each _expressionVariable As Enhancements.Basic.Expressions.ExpressionVariable In _expression.Variables
            __Variables.Add(New UsedExpressionVariable(_expressionVariable))
        Next _expressionVariable

        __ResultValue = _resultValue
        __ResultExceptionType = _resultExceptionType
    End Sub

    Public Sub New(ByVal _Text As String, ByVal _resultValue As Object, ByVal _resultExceptionType As String)
        __Text = _Text
        __Variables = New List(Of UsedExpressionVariable)()
        __ResultValue = _resultValue
        __ResultExceptionType = _resultExceptionType
    End Sub

    Private __Text As String = Nothing
    Public ReadOnly Property Text As String
        Get
            Return __Text
        End Get
    End Property

    Private __Variables As List(Of UsedExpressionVariable) = Nothing
    Public ReadOnly Property Variables As List(Of UsedExpressionVariable)
        Get
            Return __Variables
        End Get
    End Property

    Private __ResultValue As Object = Nothing
    Public ReadOnly Property ResultValue As Object
        Get
            Return __ResultValue
        End Get
    End Property

    Private __ResultExceptionType As String = Nothing
    Public ReadOnly Property ResultExceptionType As String
        Get
            Return __ResultExceptionType
        End Get
    End Property

    Public Function ToXElement() As XElement
        Dim _xElement As XElement = New XElement(XName.Get("Expression"))

        _xElement.Add(New XElement(XName.Get("Text"), Text))

        Dim _xElementVariables As XElement = New XElement(XName.Get("Variables"))
        For Each _variable As UsedExpressionVariable In Variables
            _xElementVariables.Add(_variable.ToXElement())
        Next _variable
        _xElement.Add(_xElementVariables)

        Dim _xElementResult As XElement = New XElement(XName.Get("Result"))
        If ResultValue IsNot Nothing Then
            _xElementResult.Add(New XElement(XName.Get("Value"), ResultValue.ToString()))
        Else
            If ResultExceptionType IsNot Nothing Then
                _xElementResult.Add(New XElement(XName.Get("Exception"), ResultExceptionType))
            End If
        End If
        _xElement.Add(_xElementResult)

        Return _xElement
    End Function

End Class
